package normalisation;

import java.util.ArrayList;
import normalisation.FrenchTokenizerAutomaton.Signal;

public class FrenchTokenizer {

    private FrenchTokenizerAutomaton transducer;

    public FrenchTokenizer() {
        this.transducer = new FrenchTokenizerAutomaton();
    }

    public ArrayList<String> normalize(String text) {
        return tokenize(text.toLowerCase());
    }

    public ArrayList<String> tokenize(String text) {
        char[] textContent = text.toCharArray();
        ArrayList<String> tokens = new ArrayList<String>();
        // Initialize the execution
        int begin = -1;
        transducer.reset();
        String word;
        // Run over the chars
        for (int i = 0; i < textContent.length; i++) {
            Signal s = transducer.feedChar(textContent[i]);
            switch (s) {
                case start_word:
                    begin = i;
                    break;
                case end_word:
                    word = text.substring(begin, i);
                    this.addToken(tokens, word);
                    begin = -1;
                    break;
                case end_word_prev:
                    word = text.substring(begin, i - 1);
                    this.addToken(tokens, word);
                    break;
                case switch_word:
                    word = text.substring(begin, i);
                    this.addToken(tokens, word);
                    begin = i;
                    break;
                case switch_word_prev:
                    word = text.substring(begin, i - 1);
                    this.addToken(tokens, word);
                    begin = i;
                    break;
                case cancel_word:
                    begin = -1;
                    break;
            }
        }
        // Add the last one
        if (begin != -1) {
            word = text.substring(begin, text.length());
            this.addToken(tokens, word);
        }
        return tokens;
    }

    private ArrayList<String> addToken(ArrayList<String> list, String token) {
        list.add(token);
        return list;
    }
}
